use "C:\Users\Andy Baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\chapter 3 Background\Data\intentionsmexico2006.dta", clear

drop if nationalreg~="National"
*drop if pollster=="Mexico Panel"
rename dateday date

*Mexico 2006
gen day=date(date, "MDY")

gen xarrow1e = 16990
gen yarrow1e = 35.1
gen xarrow1s = 16986
gen yarrow1s = 35.8

gen xarrow2e = 16986
gen yarrow2e = 36.9
gen xarrow2s = 16990
gen yarrow2s = 37.6

gen xarrow11e = 16820
gen yarrow11e = 0	
gen xarrow11s = 16820
gen yarrow11s = 1.5

gen xarrow12e = 16984
gen yarrow12e = 0	
gen xarrow12s = 16984
gen yarrow12s = 1.5

egen denominator=rsum(calderon madrazo obrador mercado campa other)

gen amlo_percent=100*obrador/denominator
gen calderon_percent=100*calderon/denominator
gen madrazo_percent=100*madrazo/denominator

lowess amlo_percent day, bwidth(.25) gen(amlo_lowess) nograph
lowess calderon_percent day, bwidth(.25) gen(calderon_lowess) nograph
lowess madrazo_percent day, bwidth(.25) gen(madrazo_lowess) nograph

twoway (scatter amlo_lowess calderon_lowess madrazo_lowess amlovote calderonvote madrazovote day if day>16700, connect(l l l) sort lpattern(solid dash shortdash) lcolor(black black black) ///
mc(black black black black black black) ms(i i i o o o) lwidth(medthick medthick medthick)) ///
(pcarrow yarrow1e xarrow1e yarrow1s xarrow1s , lcolor(black) mcolor(black) mfcolor(black) msize(small) lwidth(thin)) ///
(pcarrow yarrow2s xarrow2s yarrow2e xarrow2e, lcolor(black) mcolor(black) mfcolor(black) msize(small) lwidth(thin)) ///
(pcarrow yarrow11s xarrow11s yarrow11e xarrow11e, lcolor(black) mcolor(black)) ///
(pcarrow yarrow12s xarrow12s yarrow12e xarrow12e, lcolor(black) mcolor(black)), ///
legend(off) ylab(0 "0%" 10 "10%" 20 "20%" 30 "30%" 40 "40%") xtitle("{bf:Month and Panel Wave}") ytitle("{bf:Percentage of Valid Vote Intentions}") xtick(16712 16741 16771 16801 16832 16861 16892 16922 16953 16981 17012) legend(off) ///
xlab(16726 "Oct" 16725 `" " " "[ {it:pc} ]"' 16757 "Nov" 16787 "Dec" 16817 "Jan" 16847 "Feb" 16877 "Mar" 16907 "Apr" 16938 "May" 16931 `" " " "[ {it:c} ]" "' 16967 "Jun" 16996 "Jul" 17002 `" " " "[{it:e}]" "', notick labsize(medsmall)) ///
text(38 16724 "López" "Obrador", j(left) placement(x)) text(33 16780 "Calderón", j(left) placement(x)) text(27.2 16780 "Madrazo", j(left) placement(x)) ///
text(2.5 16984 "Election" "day", size(small) j(center) placement(n)) text(2 16820 "Campaign" "officially" "begins", size(small) j(center) placement(n)) ///
 graphregion(color(white)) plotregion(lstyle(yxline) lcolor(black) margin(zero)) yscale(range(0 41)) xscale(range(16711 17020)) ///
text(34.5 16990 "AMLO" "vote", size(small) j(left) placement(r)) text(38.5 16990 "Calderón" "vote", size(small) j(left) placement(r)) text(22.7 16987 "Madrazo" "vote", size(small) j(left) placement(r))
graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\chapter 3 Background\Figures\Mexico2006.tif", as(tif) replace
graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Publishers\Final Manuscript\Final Figures\BAR3_6.tif", as(tif) replace width(1500)
graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Publishers\Final Manuscript\Final Figures\BAR3_6.pdf", as(pdf) replace 

*Brazil 2002
use "C:\Users\Andy Baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\chapter 3 Background\Data\intentionsbrazil2002.dta", clear

gen day=date(date, "MDY")
egen denominator=rsum(lula serra garotinho ciro itamar Enéas ZéMaria ruicostapimenta roseana)

gen lula_percent=100*lula/denominator
gen serra_percent=100*serra/denominator
gen garotinho_percent=100*garotinho/denominator
gen ciro_percent=100*ciro/denominator
gen roseana_percent=100*roseana/denominator

lowess lula_percent day if day<15619, bwidth(.25) gen(lula_lowess) nograph
lowess serra_percent day if day<15619, bwidth(.25) gen(serra_lowess) nograph
lowess garotinho_percent day if day<15619, bwidth(.25) gen(garotinho_lowess) nograph
lowess ciro_percent day if day<15619, bwidth(.25) gen(ciro_lowess) nograph
lowess roseana_percent day if day<15619, bwidth(.25) gen(roseana_lowess) nograph

replace lula_lowess=lula_percent if day>15620
replace serra_lowess=serra_percent if day>15620

gen xarrow11e = 15522
gen yarrow11e = 0	
gen xarrow11s = 15522
gen yarrow11s = 2.2

gen xarrow12e = 15572
gen yarrow12e = 0	
gen xarrow12s = 15572
gen yarrow12s = 2.2

gen xarrow13e = 15619
gen yarrow13e = 0	
gen xarrow13s = 15619
gen yarrow13s = 2.2

gen xarrow14e = 15640
gen yarrow14e = 0	
gen xarrow14s = 15640
gen yarrow14s = 2.2

twoway (scatter lula_lowess serra_lowess garotinho_lowess ciro_lowess roseana_lowess lulavote serravote garotinhovote cirovote day if day>15323 & day<15620, connect(l l l l l) lpattern(solid dash shortdash longdash_dot solid) lcolor(black black black black black) sort ///
 mc(black black black black black black black black black) ms(i i i i i) lwidth(medthick medthick medthick medthick thin)) ///
 (scatter lula_lowess serra_lowess lulavote serravote day if day>15620, connect(l l) lpattern(solid dash) lcolor(black black) sort ///
 mc(black black black black ) ms(i i) lwidth(medthick medthick )) ///
 (pcarrow yarrow11s xarrow11s yarrow11e xarrow11e, lcolor(black) mcolor(black)) ///
(pcarrow yarrow12s xarrow12s yarrow12e xarrow12e, lcolor(black) mcolor(black)) ///
 (pcarrow yarrow13s xarrow13s yarrow13e xarrow13e, lcolor(black) mcolor(black)) ///
(pcarrow yarrow14s xarrow14s yarrow14e xarrow14e, lcolor(black) mcolor(black)), ///
 xlab(15355 "Jan" 15386 "Feb" 15415 "Mar" 15429 `" " " "[          {it:pc}         ]" "' 15446 "Apr" 15476 "May" 15508 "Jun" 15537 "Jul" 15569 "Aug" 15570 `" " " "[     {it:c}     ]" "' 15599 "Sep" 15629 "Oct"  15630 `" " " "[   {it:e}   ]" "' 15657 "Nov" 15655 `" " " "[     {it:d}     ]" "' , notick labsize(small)) ///
xtick(15340 15372 15400 15431 15461 15492 15522 15553 15584 15614 15644) legend(off) ylab(0 "0%" 10 "10%" 20 "20%" 30 "30%" 40 "40%" 50 "50%" 60 "60%") xtitle("{bf:Month and Panel Wave}") ytitle("{bf:Percentage of Valid Vote Intentions}") ///
text(35 15364 "Lula", j(left)) text(9 15362.5 "Ciro", j(left)) text(22 15436 "Serra", j(left)) text(17 15375 "Garotinho", j(left) ) ///
text(29 15374.5 "Roseana", j(left)) ///
text(9.8 15522 "Campaign" "officially" "begins", size(small) j(center) placement(s)) text(9.8 15572 "Free" "elec. hour" "begins", size(small) j(center) placement(s))  graphregion(color(white)) plotregion(lstyle(yxline) lcolor(black) margin(zero)) ///
text(9.8 15619 "Election" "day" "(1st rd.)", size(small) j(center) placement(s)) text(46.4 15621.5 "Lula" "vote", size(small) placement(r)) text(23.2 15621.5 "Serra" "vote", size(small) j(left) placement(right)) text(17.9 15621.5 "Garo." "vote", size(small) j(left) placement(right) box bcolor(white)) text(12 15621.5 "Ciro" "vote", size(small) j(left) placement(right) box bcolor(white)) ///
text(9.8 15650 "Election" "day" "(2nd rd.)", size(small) j(center) placement(s)) text(61.3 15643 "Lula" "vote", size(small) placement(r)) text(38.7 15643 "Serra" "vote", size(small) j(left) placement(right)) ///
yscale(range(0 68)) xscale(range(15340 15665))

graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\chapter 3 Background\Figures\Brazil2002.tif", as(tif) replace
graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Publishers\Final Manuscript\Final Figures\BAR3_3.tif", as(tif) replace width(1500)
graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Publishers\Final Manuscript\Final Figures\BAR3_3.pdf", as(pdf) replace 

*Brazil 2006
use "C:\Users\Andy Baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\chapter 3 Background\Data\intentionsbrazil2006.dta", clear

gen day=date(date, "MDY")

egen denominator=rsum(lula alckmin heloisa cristovam)

gen lula_percent=100*lula/denominator
gen alckmin_percent=100*alckmin/denominator
gen heloisa_percent=100*heloisa/denominator
gen cristovam_percent=100*cristovam/denominator

lowess lula_percent day if day<17075, bwidth(.25) gen(lula_lowess) nograph
lowess alckmin_percent day if day<17075, bwidth(.25) gen(alckmin_lowess) nograph
lowess heloisa_percent day if day<17075, bwidth(.25) gen(heloisa_lowess) nograph
lowess cristovam_percent day if day<17075, bwidth(.25) gen(cristovam_lowess) nograph

lowess lula_percent day if day>17075, bwidth(.25) gen(lula_lowess_rd2) nograph
lowess alckmin_percent day if day>17075, bwidth(.25) gen(alckmin_lowess_rd2) nograph
lowess heloisa_percent day if day>17075, bwidth(.25) gen(heloisa_lowess_rd2) nograph
lowess cristovam_percent day if day>17075, bwidth(.25) gen(cristovam_lowess_rd2) nograph

*replace lula_lowess=lula_percent if day>17075 
*replace alckmin_lowess=alckmin_percent if day>17075

gen xarrow11e = 16988
gen yarrow11e = 0	
gen xarrow11s = 16988
gen yarrow11s = 1.5

gen xarrow12e = 17026
gen yarrow12e = 0	
gen xarrow12s = 17026
gen yarrow12s = 1.5

gen xarrow13e = 17075
gen yarrow13e = 0	
gen xarrow13s = 17075
gen yarrow13s = 1.5

gen xarrow14e = 17103
gen yarrow14e = 0	
gen xarrow14s = 17103
gen yarrow14s = 1.5

gen xarrow15e = 17075.2
gen yarrow15e = 41.0	
gen xarrow15s = 17076
gen yarrow15s = 38.9

twoway (scatter lula_lowess alckmin_lowess heloisa_lowess lulavote alckminvote heloisavote day if day>16944, connect(l l l) lpattern(solid dash shortdash) lcolor(black black black) sort ///
 mc(black black black black black black) ms(i i i o o o ) lwidth(medthick medthick medthick medthick thin)) ///
 (scatter lula_percent alckmin_percent lulavote alckminvote day if day>17076, connect(l l) lpattern(solid dash) lcolor(black black) sort ///
 mc(black black black black) ms(i i o o) lwidth(medthick medthick medthick)) ///
  (pcarrow yarrow11s xarrow11s yarrow11e xarrow11e, lcolor(black) mcolor(black)) ///
(pcarrow yarrow12s xarrow12s yarrow12e xarrow12e, lcolor(black) mcolor(black)) ///
 (pcarrow yarrow13s xarrow13s yarrow13e xarrow13e, lcolor(black) mcolor(black)) ///
(pcarrow yarrow14s xarrow14s yarrow14e xarrow14e, lcolor(black) mcolor(black)) ///
(pcarrow yarrow15s xarrow15s yarrow15e xarrow15e, lcolor(black) mcolor(black)), ///
	xlab(16967 "Jun" 16998 "Jul" 17004 `" " " "[         {it:c}        ]" "' 17028 "Aug" 17059 "Sep" 17091 `" "Oct" "[      {it:e}       ]" "' 17115 " ", notick) ///
xtick(16953 16983 17013 17044 17075 17106) legend(off) ylab(0 "0%" 10 "10%" 20 "20%" 30 "30%" 40 "40%" 50 "50%" 60 "60%") xtitle("{bf:Month and Panel Wave}") ytitle("{bf:Percentage of Valid Vote Intentions}") ///
text(60 16950 "Lula", just(right)) text(31.5 16954.5 "Alckmin", j(left)) text(11 16963.5 "Heloísa Helena", j(left)) ///
text(7 16988 "Campaign" "officially" "begins", size(vsmall) j(center) placement(s)) text(7 17026 "Free" "elec. hour" "begins", size(vsmall) j(center) placement(s)) ///
graphregion(color(white)) plotregion(lstyle(yxline) lcolor(black) margin(zero)) ///
text(5 17075 "Election day" "(1st rd.)", size(vsmall) j(center) placement(s)) text(48.6 17076 "Lula" "vote", size(small) j(left) placement(r)) ///
text(38 17076 "Alckmin" "vote", size(small) j(left) placement(r)  box bcolor(white) margin(zero)) ///
text(8.2 17076 "HH" "vote", size(small) j(left) placement(r)) ///
text(60.8 17104 "Lula" "vote", size(small) j(left) placement(r)) text(39.17 17104 "Alckmin" "vote", size(small) j(left) placement(r)) ///
text(7 17103 "Election" "day" "(2nd rd.)", size(vsmall) j(center) placement(s)) ///
yscale(range(0 65)) xscale(range(16940 17120))

graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\chapter 3 Background\Figures\Brazil2006.tif", as(tif) replace
graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Publishers\Final Manuscript\Final Figures\BAR3_4.tif", as(tif) replace width(1500)
graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Publishers\Final Manuscript\Final Figures\BAR3_4.pdf", as(pdf) replace 


*Brazil 2014
use "C:\Users\Andy Baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\chapter 3 Background\Data\intentionsbrazil2014.dta", clear

gen day=date(date, "MDY")

egen denominator=rsum(dilma aecioneves psbcampossilva pereira genro otros)

gen dilma_percent=100*dilma/denominator
gen neves_percent=100*aecioneves/denominator
gen psbcampossilva_percent=100*psbcampossilva/denominator

lowess dilma_percent day if day<20001, bwidth(.25) gen(dilma_lowess) nograph
lowess neves_percent day  if day<20001, bwidth(.25) gen(neves_lowess) nograph
lowess psbcampossilva_percent day if day<20001, bwidth(.25) gen(psdbcampossilva_lowess) nograph

replace dilma_lowess=dilma_percent if day>20002 
replace neves_lowess=neves_percent if day>20002

*lowess dilma_percent day if day>20002, bwidth(.25) gen(dilma_lowess2) nograph
*lowess neves_percent day  if day>20002, bwidth(.25) gen(neves_lowess2) nograph

*replace dilma_lowess=dilma_lowess2 if day>20002 
*replace neves_lowess=neves_lowess2 if day>20002

gen xarrow10s = 19948
gen yarrow10s = 16.5
gen xarrow10e = 19948
gen yarrow10e = 22

gen xarrow11e = 19910
gen yarrow11e = 0	
gen xarrow11s = 19910
gen yarrow11s = 2

gen xarrow12e = 19954
gen yarrow12e = 0	
gen xarrow12s = 19954
gen yarrow12s = 2

gen xarrow13e = 20001
gen yarrow13e = 0	
gen xarrow13s = 20001
gen yarrow13s = 2

gen xarrow14e = 20022
gen yarrow14e = 0	
gen xarrow14s = 20022
gen yarrow14s = 2

twoway (scatter dilma_lowess neves_lowess psdbcampossilva_lowess dilmavote nevesvote marinavote day if day<20002, connect(l l l) lpattern(solid dash shortdash) lcolor(black black black) mc(black black black black black black) ms(i i i) lwidth(medthick medthick medthick)) ///
(scatter dilma_lowess neves_lowess dilmavote nevesvote day if day>20002, connect(l l) sort lpattern(solid dash) lcolor(black black) mc(black black black black) ms(i i) lwidth(medthick medthick)) ///
(pcarrow yarrow10s xarrow10s yarrow10e xarrow10e, lcolor(black) mcolor(black)) ///
(pcarrow yarrow11s xarrow11s yarrow11e xarrow11e, lcolor(black) mcolor(black)) ///
(pcarrow yarrow12s xarrow12s yarrow12e xarrow12e, lcolor(black) mcolor(black)) ///
 (pcarrow yarrow13s xarrow13s yarrow13e xarrow13e, lcolor(black) mcolor(black)) ///
(pcarrow yarrow14s xarrow14s yarrow14e xarrow14e, lcolor(black) mcolor(black)), ///
xlab(19769 "Feb" 19798 "Mar" 19829 "Apr" 19859 "May" 19875 `" " " "[      {it:pc}     ]" "' 19890 "Jun" 19920 "Jul" 19925 `" " " "[{it:c2}]" "' 19951 "Aug" 19966 `" " " "[{it:c3}]" "' 19982 "Sep" 19987 `" " " "[{it:c4}]" "' 19997 `" " " "[{it:c5}]" "' 20013 "Oct" 20007 `" " " "[{it:e6}]" "' 20032 `" " " "[{it:e7}]" "'  20039 " ", notick labs(small)) ///
xtick(19755 19782 19814 19844 19875 19905 19935 19967 19997 20028) legend(off) ylab(0 "0%" 10 "10%" 20 "20%" 30 "30%" 40 "40%" 50 "50%" 60 "60%") xtitle("{bf:Month and Panel Wave}") ytitle("{bf: Percentage of Valid Vote Intentions}") ///
text(60 19769 "Dilma", placement(x)) text(26 19771 "Aécio", placement(x)) text(12.5 19769 "Campos", placement(x)) text(30.5 19960 "Marina", placement(x))  graphregion(color(white)) plotregion(lstyle(yxline) lcolor(black) margin(zero)) ///
text(41.59 20003.5 "Dilma" "vote", size(small) j(left) placement(r)) ///
text(33.55 20003.5 "Aécio" "vote", size(small) j(left) placement(r)) ///
text(21.3 20003.5 "Marina" "vote", size(small) j(left) placement(r)) ///
text(17 19948 "Campos" "dies", size(small) j(center) placement(s)) /// 
text(9 19910 "Campaign" "officially" "begins", size(small) j(center) placement(s)) ///
text(9 19954 "Free" "elec. hour" "begins", size(small) j(center) placement(s)) ///
text(9 20001 "Election" "day" "(1st rd.)", size(small) j(center) placement(s)) ///
text(9 20030 "Election" "day" "(2nd rd.)", size(small) j(center) placement(s)) ///
text(52.9 20024.5 "Dilma" "vote", size(small) j(left) placement(r)) ///
text(47.4 20024.5 "Aécio" "vote", size(small) j(left) placement(r)) ///
xscale(range(19755 20045)) yscale(range(0 64))

graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\chapter 3 Background\Figures\Brazil2014.tif", as(tif) replace
graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Publishers\Final Manuscript\Final Figures\BAR3_5.tif", as(tif) replace width(1500)
graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Publishers\Final Manuscript\Final Figures\BAR3_5.pdf", as(pdf) replace 

*FIGURE 1.1
twoway (scatter dilma_lowess neves_lowess psdbcampossilva_lowess dilmavote nevesvote marinavote day if day<20002, connect(l l l) lpattern(solid dash shortdash) lcolor(black black black) mc(black black black black black black) ms(i i i) lwidth(medthick medthick medthick)) ///
(scatter dilma_lowess neves_lowess dilmavote nevesvote day if day>20002, connect(l l) sort lpattern(solid dash) lcolor(black black) mc(black black black black) ms(i i) lwidth(medthick medthick)) ///
(pcarrow yarrow10s xarrow10s yarrow10e xarrow10e, lcolor(black) mcolor(black)) ///
(pcarrow yarrow11s xarrow11s yarrow11e xarrow11e, lcolor(black) mcolor(black)) ///
(pcarrow yarrow12s xarrow12s yarrow12e xarrow12e, lcolor(black) mcolor(black)) ///
 (pcarrow yarrow13s xarrow13s yarrow13e xarrow13e, lcolor(black) mcolor(black)) ///
(pcarrow yarrow14s xarrow14s yarrow14e xarrow14e, lcolor(black) mcolor(black)), ///
xlab(19769 "Feb" 19798 "Mar" 19829 "Apr" 19859 "May" 19890 "Jun" 19920 "Jul" 19951 "Aug" 19982 "Sep" 20013 "Oct" 20039 " ", notick labs(medsmall)) ///
xtick(19755 19782 19814 19844 19875 19905 19935 19967 19997 20028) legend(off) ylab(0 "0%" 10 "10%" 20 "20%" 30 "30%" 40 "40%" 50 "50%" 60 "60%") xtitle("{bf:Month}") ytitle("{bf: Percentage of Valid Vote Intentions}") ///
text(60 19769 "Dilma", placement(x)) text(26 19771 "Aécio", placement(x)) text(12.5 19769 "Campos", placement(x)) text(30.5 19960 "Marina", placement(x))  graphregion(color(white)) plotregion(lstyle(yxline) lcolor(black) margin(zero)) ///
text(41.59 20003.5 "Dilma" "vote", size(small) j(left) placement(r)) ///
text(33.55 20003.5 "Aécio" "vote", size(small) j(left) placement(r)) ///
text(21.3 20003.5 "Marina" "vote", size(small) j(left) placement(r)) ///
text(17 19948 "Campos" "dies", size(small) j(center) placement(s)) /// 
text(9 19910 "Campaign" "officially" "begins", size(small) j(center) placement(s)) ///
text(9 19954 "Media" "advertising" "begins", size(small) j(center) placement(s)) ///
text(9 20001 "Election" "day" "(1st rd.)", size(small) j(center) placement(s)) ///
text(9 20030 "Election" "day" "(2nd rd.)", size(small) j(center) placement(s)) ///
text(52.9 20024.5 "Dilma" "vote", size(small) j(left) placement(r)) ///
text(47.4 20024.5 "Aécio" "vote", size(small) j(left) placement(r)) ///
xscale(range(19755 20045)) yscale(range(0 64))

graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 1 Intro\Figures\Brazil2014.tif", as(tif) replace
graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Publishers\Final Manuscript\Final Figures\BAR1_1.tif", as(tif) replace width(1500)
graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Publishers\Final Manuscript\Final Figures\BAR1_1.pdf", as(pdf) replace 

